package similarity;
import java.util.ArrayList;
public class SimilarityMain {
	float sim(ArrayList<Float> va, ArrayList<Float> vb)
	{
		if (va.size() != vb.size())
		{
			return 0;
		}
		
		int size = va.size();
		float simVal = 0;
	
		float num = 0;
		float den = 1;
		float a = 0;
		float b = 0;
		for(int i = 0;i < size;i++){
			num = va.get(i)*vb.get(i)+num;
			a = (float) Math.pow(va.get(i),2) + a;
			b = (float) Math.pow(vb.get(i),2) + b;
		}
		den = (float) (Math.sqrt(a)* Math.sqrt(b));
		
		simVal = num / den;
		return simVal;
	}
	
	public static void main(String[] args) {
		String item[] = {"吃苹果", "逛商店", "看电视剧", "打羽毛球", "吃桔子"};
		float a[] = {(float) 3.5, 5, 5, 5,0};
		float b[] = {3, 5, 4, 5, 5};
		ArrayList<String> vitem = new ArrayList<String>();
		ArrayList<Float> va = new ArrayList<Float>();
		ArrayList<Float> vb = new ArrayList<Float>();
		for (int i = 0; i < a.length; i++)
		{
			vitem.add(item[i]);
			va.add(new Float(a[i]));
			vb.add(new Float(b[i]));
		}
		System.out.print("兴趣");
		System.out.println(vitem);
		System.out.print("小红");
		System.out.println(va);
		System.out.print("邹恩岑");
		System.out.println(vb);
		
		SimilarityMain sim = new SimilarityMain();
	
		float simVal = sim.sim(va, vb);
		
		System.out.println("The sim value is:" + simVal);
	}
}
